import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.TimeZone;

/**
 * Capitol Reef Team, Summer 2012 [ICS 314]
 *
 * This class file consists of a simple program that reads in user inputs and
 * creates a .ics (calendar file) and follows the standards outlined in the RFC 5445.
 * 
 * Support has been added for the following parameters of a ICS file, please
 * refer to RFC 5445 for specific requirements.
 * 
 * Version (Section 3.7.4 in RFC 5445)
 * Property Name: VERSION
 *
 * Purpose: This property specifies the identifier corresponding to the highest version number or the minimum and maximum range of the iCalendar specification that is required in order to interpret the iCalendar object.
 *
 * Value Type: TEXT
 *
 * Property Parameters: IANA and non-standard property parameters can be specified on this property.
 *
 * Conformance: This property MUST be specified once in an iCalendar object.
 *
 * Description: A value of "2.0" corresponds to this memo.
 *
 * Format Definition: This property is defined by the following notation:
 *
 * version    = "VERSION" verparam ":" vervalue CRLF
 *
 * verparam   = *(";" other-param)
 *
 * vervalue   = "2.0"         ;This memo
 *                            / maxver
 *                            / (minver ";" maxver)
 *
 *                            minver     = <A IANA-registered iCalendar version identifier>
 *                            ;Minimum iCalendar version needed to parse the iCalendar object.
 *
 *                            maxver     = <A IANA-registered iCalendar version identifier>
 *                            ;Maximum iCalendar version needed to parse the iCalendar object.
 * 
 * Example:  The following is an example of this property: VERSION:2.0
 * 
 * 
 * 
 *  
 * 
 * 
 * Classification (3.8.1.3)
 * Comment (3.8.14)
 * Priority (3.8.1.9)
 * Location (3.8.1.7)
 * Resources (3.8.1.10)
 * DTSTART (3.8.2.4)
 * DTEND (3.8.2.2)
 * Time zone identifer (3.8.3.1)
 * 
 * @author James Knight
 * @author Allan Wu
 * @author Jerry Koko
 *
 */
public class CapitolReefCalendar {
	
	// This is necessary to format the timestamp to iCal specs (Allan)
	private static SimpleDateFormat iCalDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmm'00'");
	 

	public static void main(String args[]) throws IOException
	{
		
		// variable setup. accepts data from user used later on
		String data = null;
		int year = 0;
		int month = 0;
		int date = 0;
		int minutes = 00;
		int hourOfDay = 00;
		int second = 00; // except this variable always stays '00'
		
		// the try-catch nest is to prevent data entry errors from breaking the program 
		try {
		
		// Time Stamp
		Calendar cal = new GregorianCalendar();
	    Date timeStamp = cal.getTime();
		TimeZone timeZone = cal.getTimeZone();
		
		// Get the difference of hours from GMT
	    int ZONE_OFFSET = timeZone.getRawOffset();
	    int GMT_Adjust = ZONE_OFFSET / 3600000;
    
	    
		// This is the Command-line version of Calendar
		Scanner input = new Scanner(System.in);
		
		// Event is just stored as a string 
		System.out.println("Name of Event: ");
		System.out.println();
		String eventName = input.nextLine();
		
		// Begin Event's start time computations 
		Date startTime = cal.getTime();
		System.out.println("Enter the Event's Start Time yyyyMMddHHmm (with military or 24-hour format 'HHmm'): ");
		data = input.nextLine();
		year = Integer.parseInt(data.substring(0, 4));
		month = Integer.parseInt(data.substring(4, 6));
		month = month - 1;
		date = Integer.parseInt(data.substring(6, 8));
		hourOfDay = Integer.parseInt(data.substring(8, 10));
		hourOfDay = hourOfDay - GMT_Adjust;
		minutes = Integer.parseInt(data.substring(10, 12));
		cal.set(year, month, date, hourOfDay, minutes, second);

		// finally, we compute the start time adjusted for GMT differences
		startTime = cal.getTime();
		
	
		
		// Begin computing the Event's ending time
		System.out.println("Enter the Events Ending Time yyyyMMddHHmm (with military or 24-hour format 'HHmm'): ");
		data = input.nextLine();
		year = Integer.parseInt(data.substring(0, 4));
		month = Integer.parseInt(data.substring(4, 6));
		month = month - 1;
		date = Integer.parseInt(data.substring(6, 8));
		hourOfDay = Integer.parseInt(data.substring(8, 10));
		hourOfDay = hourOfDay - GMT_Adjust;
		minutes = Integer.parseInt(data.substring(10, 12));
		cal.set(year, month, date, hourOfDay, minutes, second);
		
		//finally we compute the events end time
		Date endTime = cal.getTime();
		
		
		// append the .ics file name suffix
		System.out.println("Name of file: ");
		System.out.println();
		String name = input.nextLine();
		name = name + ".ics";  // appends .ics suffix
		

// this string builder is compatible with iCal specifications (we can add more if necessary--Allan)
String calendarContent =
        "BEGIN:VCALENDAR\n" +
                "METHOD:REQUEST\n" +
                "PRODID:\n" +
                "VERSION:2.0\n" +
                "BEGIN:VEVENT\n" +
                "DTSTAMP:" + iCalDateFormat.format(timeStamp) + "Z\n" +
                "DTSTART:" + iCalDateFormat.format(startTime) + "Z\n" +
                "DTEND:"  + iCalDateFormat.format(endTime) + "Z\n" +
                "SUMMARY:" + eventName + "\n" +
                "UID:\n" +
                "ATTENDEE:\n" +
                "ORGANIZER:MAILTO:organizer@yahoo.com\n" +
                "LOCATION:\n" +
                "DESCRIPTION:\n" +
                "SEQUENCE:0\n" +
                "PRIORITY:5\n" +
                "CLASS:PUBLIC\n" +
                "STATUS:CONFIRMED\n" +
                "TRANSP:OPAQUE\n" +
                "BEGIN:VALARM\n" +
                "ACTION:DISPLAY\n" +
                "DESCRIPTION:REMINDER\n" +
                "TRIGGER;RELATED=START:-PT00H15M00S\n" +
                "END:VALARM\n" +
                "END:VEVENT\n" +
                "END:VCALENDAR";

	// We can list the file contents with this next line 
	System.out.println(calendarContent);


	// File output section  	
  	FileWriter fw = new FileWriter(name);
  	BufferedWriter bw = new BufferedWriter(fw);
  	PrintWriter outFile = new PrintWriter(bw);
  
  	// Produces a file 
  	outFile.println(calendarContent);
  
  	// Close file stream
  	outFile.close();
  
  	// Interface response to User
  	System.out.println();
  	System.out.println("Success! An output file has been created: " + name);
  
	}
  	
	// or unless the user input is wrong, it responds with this...	
	catch (Exception e) {
		System.out.println("Opps, some wrong information information was entered. Please try again later! ");
	}
	
		
}
	
}